<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="会议主题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入会议主题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="会议地点" prop="address">
          <el-input
            v-model="queryParams.address"
            placeholder="请输入会议地点"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="主持人" prop="anchor">
          <el-input
            v-model="queryParams.anchor"
            placeholder="请输入主持人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择状态">
            <el-option
              v-for="item in dict.type.daily_work_report_status"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="dateRange">
          <el-date-picker
            v-model="queryParams.dateRange"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00','23:59:59']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <TableCard>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['cooperate:meetingMinutes:add']"
            >新增
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

          <el-table :border="Global.tableShowBorder" v-loading="loading" :data="list">
            <el-table-column label="会议主题" align="center" prop="title"/>
            <el-table-column label="会议地点" align="center" prop="address"/>
            <el-table-column label="主持人" align="center" prop="anchor"/>
            <el-table-column label="会议时间" align="center" prop="meetingTime"></el-table-column>
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="{row}">
                <dict-tag :options="dict.type.daily_work_report_status" :value="row.status"/>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
              <template slot-scope="{row}">
                <el-button v-if="row.status == 1"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(row)"
                  v-hasPermi="['cooperate:meetingMinutes:edit']"
                >修改
                </el-button>
                <el-button v-if="row.status == 1"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(row)"
                  v-hasPermi="['cooperate:meetingMinutes:remove']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
      </TableCard>
      <!-- 添加或修改对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="会议主题" prop="title">
                <el-input v-model="form.title" placeholder="请输入会议主题"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="会议地点" prop="address">
                <el-input v-model="form.address" placeholder="请输入会议地点"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="主持人" prop="anchor">
                <el-input v-model="form.anchor" placeholder="请输入主持人"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参会人员" prop="participants">
                <!-- <el-select v-model="form.participants" multiple filterable clearable placeholder="请选择参会人员">
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId">
                  </el-option>
                </el-select> -->
                <div class="" style="width: 100%;">
                  <el-input v-model="form.participants" clearable  placeholder="请输入主持人"/>
                  <div style="color:red;">*(用英文逗号,间隔多个参会人员，末尾不可逗号结尾，例如老王,老郭)</div>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内容记录" prop="content">
                <el-input v-model="form.content" type="textarea" :rows="6" placeholder="请输入内容记录"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="会议结论及后续行动" prop="result">
                <el-input v-model="form.result" type="textarea" :rows="6" placeholder="请输入会议结论及后续行动"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="会议时间" prop="meetingTime">
                <el-date-picker
                  v-model="form.meetingTime"
                  type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="图片" prop="images">
                <imageUpload @fileSuccess="fileSuccess"
                  :fileType="['bmp','jpg','png','gif',]" returnData="Array" fileName="files" :fileData="{'bizType':'meetingMinutes',}"
                  action="/oa-pcapi/cooperate/file/uploadAtt"
                  v-model="form.images" :limit="100"></imageUpload>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio  v-for="item in dict.type.daily_work_report_status"
                  :key="item.value"
                  :label="item.value">{{item.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="附件" prop="attachment"><el-button type="primary" icon="el-icon-plus" @click="addList('attachment')"></el-button></el-form-item>
          <el-row v-for="(item,index) in form.attachment" :key="'attachment' + index">
            <el-col :span="24">
              <!--
              :rules="{
                required: true, message: '文件名称不能为空', trigger: 'blur'
              }" -->
              <el-form-item label="文件名称"
              :prop="'attachment.' + index + '.fileName'"
              >
                <el-input v-model="item.fileName"  placeholder="请输入文件名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <!--
              :rules="{
                required: true, message: '文件路径不能为空', trigger: 'blur'
              }" -->
              <el-form-item label="文件路径"
              :prop="'attachment.' + index + '.url'"
              >
                <div class="flex_hcneter">
                  <FileUpload :limit="1"  v-model="item.url"
                  :fileType="null" returnData="Array"
                  fileName="files" :fileData="{'bizType':'meetingMinutes',}"
                  action="/oa-pcapi/cooperate/file/uploadAtt" btnName="选择"></FileUpload>
                  <el-button style="margin-left:10px;" v-if="form.attachment.length > 1"
                  @click="delList(index,'attachment')" type="primary" icon="el-icon-minus"></el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="报送人" prop="cpmMeetingMinutesUserList"><el-button type="primary" icon="el-icon-plus" @click="addList('cpmMeetingMinutesUserList')"></el-button></el-form-item>
          <el-row v-for="(item,index) in form.cpmMeetingMinutesUserList" :key="'cpmMeetingMinutesUserList' + index">
            <el-col :span="12">
              <el-form-item label="类型"
              :prop="'cpmMeetingMinutesUserList.' + index + '.category'">
                <el-select v-model="item.category" filterable clearable placeholder="请选择参会人员">
                  <el-option
                    v-for="item1 in dict.type.meet_minute_type"
                    :key="item1.value"
                    :label="item1.label"
                    :value="item1.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户"
              :prop="'cpmMeetingMinutesUserList.' + index + '.userId'">
                <div class="flex_hcneter">
                  <el-select v-model="item.userId" filterable clearable placeholder="请选择参会人员">
                    <el-option
                      v-for="item1 in userList"
                      :key="item1.userId"
                      :label="item1.userName"
                      :value="Number(item1.userId)">
                    </el-option>
                  </el-select>
                  <el-button style="margin-left:10px;" v-if="form.cpmMeetingMinutesUserList.length > 1"
                  @click="delList(index,'cpmMeetingMinutesUserList')" type="primary" icon="el-icon-minus"></el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import {listMeetingMinutes, getMeetingMinutes, addMeetingMinutes, updateMeetingMinutes, delMeetingMinutes} from "@/api/collaWorryFree/meetMinute";
import {userListAll} from "@/api/system/user";
import { aW } from '@fullcalendar/core/internal-common';
export default {
  name: "MeetMinute",
  dicts: ['daily_work_report_status','meet_minute_type',],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
        // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,dateRange:[],address: undefined,
        status: undefined,anchor: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [ {required: true, message: "会议主题不能为空", trigger: "blur"} ],
        // address: [ {required: true, message: "会议地点不能为空", trigger: "blur"} ],
        // anchor: [ {required: true, message: "主持人不能为空", trigger: "blur"} ],
        // participants: [ {required: true, message: "参会人员不能为空", trigger: "change"} ],
        // content: [ {required: true, message: "内容记录不能为空", trigger: "blur"} ],
        // meetingTime: [ {required: true, message: "会议时间不能为空", trigger: "change"} ],
        // status: [ {required: true, message: "状态不能为空", trigger: "change"} ],
      },
      userList:[],//用户数据
    };
  },
  created() {
      this.getList();
  },
  methods: {
    addList(ziduan){//添加列表
      this.form[ziduan].push({})
    },
    delList(index,ziduan){//删除列表
      this.form[ziduan].splice(index,1)
    },
    async userListAll(){//获取所有用户
      let res = await userListAll()
      let data = res.data || []
      this.userList = data.map(item=>{
        return{...item,userId:item.userId + ''}
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      let dateRange = queryParams.dateRange || []
      if(dateRange.length) {
        queryParams.meetingTimeStart = dateRange[0]
        queryParams.meetingTimeEnd = dateRange[1]
      }
      delete queryParams.dateRange
      listMeetingMinutes(queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {status:'1',cpmMeetingMinutesUserList:[{}],attachment:[{}],};
      this.resetForm("form");
      this.userListAll()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    fileSuccess(e){//图片上传成功
      console.log('fileSuccess',e)
      let imageList = e || []
      this.form.imageList = imageList.map(item=>{
        return{...item,fileName:item.name,}
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会议纪要";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMeetingMinutes(id).then(response => {
        let data = response.data || {};
        let images = data.images || []
        let imagess = images.map(item=>item.url).toString()
        this.form = {
          ...data,
          // participants:data.participants ? data.participants.split(',') : [],
          cpmMeetingMinutesUserList:data.cpmMeetingMinutesUserList || [{}],attachment:data.attachment || [{}],
          images:imagess,
          imageList:images,
        }
        this.open = true;
        this.title = "修改会议纪要";
      });
    },
    geshihuadh(str){//格式化逗号格式车牌和车位号
      let newStr = this.form[str] || ''
      newStr = newStr.replace(/\s+/g, '');
      let newStrs = newStr.replace(/，/g, ',');
      let strList = newStrs.split(',')
      strList = strList.filter(item=>item)
      let zuihou = strList.toString()
      return zuihou
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form = {...this.form}
          form.participants = this.geshihuadh('participants')
          form.images = [...form.imageList]
          if (this.form.id != undefined) {
            updateMeetingMinutes(form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMeetingMinutes(form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
          return delMeetingMinutes(id);
      }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
